package com.example.homelicaisystem.config;


import com.example.homelicaisystem.utils.UserUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("Authorization");
        int userId = UserUtil.getUserIdByToken(token);

        //保存当前用户
        UserUtil.saveUser(userId);
        //在返回的头里面设置新的token
        response.setHeader("Authorization", UserUtil.getToken(userId));
        return true;
    }
}
